package com.woniu.util;

import com.woniu.pojo.Activity;
import com.woniu.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ActivityScheduler {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每隔1分钟更新一次活动状态
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    public void updateActivity(){
        List<Activity> activities = activityService.selectAll("activityAll");
        System.out.println(activities.get(3).getCaStartTime());
        //获取现在时间
        Date nowTime = new Date();
        for(Activity activity:activities){
            //获取活动时间
            Date startTime = activity.getCaStartTime();
            Date endTime = activity.getCaEndTime();
            //判断,定时重置状态
            if(nowTime.before(endTime) && nowTime.after(startTime) && activity.getCaStatus()!=1){
                activity.setCaStatus(1);
                activityService.updateActivity(activity,"activityAll");
//                redisTemplate.delete("activityAll");
            }else if(nowTime.after(endTime) && activity.getCaStatus()!=2){
                activity.setCaStatus(2);
                activityService.updateActivity(activity,"activityAll");
//                redisTemplate.delete("activityAll");
            }
        }
    }

}
